﻿using System;
using System.Web.UI;
using Vega.USiteBuilder;

namespace kelvinDigital.USiteBuilderAdmin
{
    public partial class uSiteBuilderAdmin : UserControl
    {
        protected void btnCheck_Click(object sender, EventArgs e)
        {
            BindGrids();

            docTypeTab.Visible = cbxDocTypes.Checked;
            doctypes.Visible = cbxDocTypes.Checked;
            templateTab.Visible = cbxTemplates.Checked;
            templates.Visible = cbxTemplates.Checked;
            dataTypeTab.Visible = cbxDataTypes.Checked;
            dataTypes.Visible = cbxDataTypes.Checked;

            int numVisible = (cbxDocTypes.Checked ? 1 : 0) + (cbxTemplates.Checked ? 1 : 0) + (cbxDataTypes.Checked ? 1 : 0);

            if (numVisible > 1)
            {
                tabs.Visible = true;

                if (cbxDataTypes.Checked)
                {
                    dataTypeTab.Attributes["class"] = "active";
                    dataTypes.Attributes["class"] = "tab-pane active";
                    docTypeTab.Attributes["class"] = "";
                    doctypes.Attributes["class"] = "tab-pane";
                }
                else if (cbxDocTypes.Checked)
                {
                    docTypeTab.Attributes["class"] = "active";
                    doctypes.Attributes["class"] = "tab-pane active";
                } 
                
                templateTab.Attributes["class"] = "";
                templates.Attributes["class"] = "tab-pane";
            }
            else
            {
                tabs.Visible = false;
                if (cbxDataTypes.Checked)
                {
                    dataTypes.Attributes["class"] = "tab-pane active";
                }
                else if (cbxDocTypes.Checked)
                {
                    doctypes.Attributes["class"] = "tab-pane active";

                }
                else
                {
                    templates.Attributes["class"] = "tab-pane active";
                }
            }


            results.Visible = true;
            btnSyncAll.Visible = true;
        }

        protected void btnSync_Click(object sender, EventArgs e)
        {
            if (cbxDataTypes.Checked)
            {
                UmbracoManager.SynchronizeAllDataTypes();
            }

            if (cbxTemplates.Checked)
            {
                UmbracoManager.SynchronizeAllTemplates();
            }

            if (cbxDocTypes.Checked)
            {
                UmbracoManager.SynchronizeAllDocumentTypes();
                //UmbracoManager.CleanUpDocumentTypes();
            }
    
            BindGrids();
        }

        protected void RebindGrids(object sender, EventArgs e)
        {
            BindGrids();
        }

        private void BindGrids()
        {
            if (cbxDataTypes.Checked)
            {
                rsltDataTypes.DataSource = UmbracoManager.PreviewDataTypeChanges();
                rsltDataTypes.ParentAlias = "";
                rsltDataTypes.BindGrid();
            }

            if (cbxDocTypes.Checked)
            {
                rsltDocType.DataSource = UmbracoManager.PreviewDocumentTypeChanges();
                rsltDocType.ParentAlias = "";
                rsltDocType.BindGrid();
            }

            if (cbxTemplates.Checked)
            {
                rsltTemplates.DataSource = UmbracoManager.PreviewTemplateChanges();
                rsltTemplates.ParentAlias = "";
                rsltTemplates.BindGrid();
            }

        }
    }
}